//
//  HistoryViewController.m
//  GoldOperationClient
//
//  Created by yaxue wang on 16/8/4.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "HistoryViewController.h"
#import "HistoryTableCell.h"
#import "historyModel.h"
#import "HistoryView.h"
#import "TPKeyboardAvoidingScrollView.h"
#import "BankView.h"
#import "ScreentoneViewController.h"
#import "SuspensionView.h"
@interface HistoryViewController()<UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource,UITextViewDelegate>
{
    TPKeyboardAvoidingScrollView *_scrollView;
    UILabel *_rightLab;
    UIPickerView*_pickerView;
    BankView *_bgView;
    NSString *_trame;
    double _longitude;
    double _latitude;
    NSString *_type;
    NSString *_bussiness;
    NSMutableArray *_typeAllAr;
}
@property (nonatomic, strong)UIButton *sendBtn;
@property (nonatomic, strong)CTextField *nameTF;
@property (nonatomic, strong)NSString *buiness;
@property (nonatomic, strong)UILabel *typeLab;
@property (nonatomic, strong)NSString *typeStr;
@property (nonatomic, strong)UILabel *codeLab;
@property (nonatomic, strong)UILabel *timeLab;
@property (nonatomic, strong)UILabel *trameLab;
@property (nonatomic, strong)UITextView *remarkView;
@property (nonatomic, strong)UIView *textBGView;
@property (nonatomic, strong)UIPickerView*pickerView;
@property(nonatomic, strong)NSMutableArray*dataAr;
@property(nonatomic, strong)NSMutableArray *typeAr;
@property(nonatomic, strong)UIView*addView;
@property(nonatomic)BOOL isConext;//是否在编辑
@property(nonatomic)BOOL isMachineLink;
//@property(nonatomic)BOOL isHidden;
@property(nonatomic, strong)UIView *ringView;
@property(nonatomic, strong)UIView *editView;
@end
@implementation HistoryViewController
- (UIStatusBarStyle)preferredStatusBarStyle{
    return  UIStatusBarStyleDefault;
}

- (void)initView
{
    [super initView];
    self.navBar.hidden=NO;
//    _isHidden=NO;
    _isConext=NO;
    _isMachineLink=NO;
    [self.navBar setBgColor:[UIColor clearColor]];
     UIView*narView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 64)];
    narView.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:narView];
    [narView resignFirstResponder];
    __block typeof(self) blockself = self;
    UIView *backView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 64)];
    [narView addSubview:backView];
    [backView bk_whenTapped:^{
        if(self.Type==MachineLink)//不需要关联
        {
            if (_isConext==YES) {
                
                [CClass showAlertTitle:nil hidden:NO context:@"离开后报修内容丢失。是否离开？" oneBtn:@"离开"twoBtn:@"取消" handler:^{
                    [blockself backController];
                }handler:^{
                    
                }];
            }else
            {
                [blockself backController];

            }
            
            
        }else if(self.Type==MachineUnLink)//需要关联
        {
            if (_isMachineLink==NO) {//没有关联的时候
                [CClass showAlertTitle:nil hidden:NO context:@"是否将该设备关联到该报修单？" oneBtn:@"关联" twoBtn:@"取消" handler:^{
                    //录入新设备的请求
                    [self requsetMachineInput];
                } handler:^{
                    [blockself backController];
                }];
            }
            if (_isConext==YES) {
                
                [CClass showAlertTitle:nil hidden:NO context:@"离开后报修内容丢失。是否离开？" oneBtn:@"离开"twoBtn:@"取消" handler:^{
                    [blockself backController];
                }handler:^{
                    
                }];
            }
            
        }
        
        
        
       
        
    }];
    
    UIImage *icoBack = [UIImage imageNamed:@"Ico_nar_back"];
    UIImageView *imgBack = [[UIImageView alloc] initWithFrame:CGRectMake(15, 33, icoBack.size.width, icoBack.size.height)];
    imgBack.image=icoBack;
    [backView addSubview:imgBack];
    
    UILabel *backTitle = [[UILabel alloc] init];
    backTitle.frame=CGRectMake(CGRectGetMaxX(imgBack.frame) + 5, 20, 50 - imgBack.frame.size.width,44);
    
    [backTitle setText:@"返回"];
    [backTitle setTextColor:MainColor];
    [backTitle setFont:F(14)];
    [backView addSubview:backTitle];
    
    UIView *bgView =[[UIView alloc]init];
    if(WindowWidth==320)
    {
        bgView.frame=CGRectMake((WindowWidth-140)/2, 30, 140, 25);
    }else
    {
        bgView.frame=CGRectMake((WindowWidth-200)/2, 30, 200, 25);
    }
    bgView.layer.masksToBounds=YES;
    bgView.layer.cornerRadius=4;
    bgView.backgroundColor =C(@"ebe9e5");
    [narView addSubview:bgView];
    UIButton *btn1=[UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame =CGRectMake(0, 0, bgView.bounds.size.width/2, bgView.bounds.size.height);
    [btn1 setTitle:@"设备信息" forState:UIControlStateNormal];
    btn1.titleLabel.font =F(12);
    [bgView addSubview:btn1];
    [btn1 setBackgroundColor:MainColor];
    [btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    UIButton *btn2=[UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame =CGRectMake(bgView.bounds.size.width/2, 0, bgView.bounds.size.width/2, bgView.bounds.size.height);
    [btn2 setTitle:@"维护记录" forState:UIControlStateNormal];
    btn2.titleLabel.font =F(12);
    [bgView addSubview:btn2];
    [btn2 setBackgroundColor:C(@"e8e8e8")];
    [btn2 setTitleColor:C(@"5a5a5a") forState:UIControlStateNormal];
    [btn1 bk_addEventHandler:^(id sender) {
        
        if (self.num==98||self.num==99||self.num==97||self.num==1000||self.Type==MachineUnLink){
            _ringView.hidden=NO;
            
        }else
        {
            if (_isConext==YES) {
                _addView.hidden=YES;
                _editView.hidden=NO;
            }else
            {
                _addView.hidden=NO;
                _editView.hidden=YES;
            }
            
        }
        [btn1 setBackgroundColor:MainColor];
        [btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btn2.backgroundColor=C(@"e8e8e8");
        [btn2 setTitleColor:C(@"5a5a5a") forState:UIControlStateNormal];
        _scrollView.contentOffset =CGPointMake(0, 0);
    } forControlEvents:UIControlEventTouchUpInside];
    [btn2 bk_addEventHandler:^(id sender) {
        if (self.num==98||self.num==99||self.num==97||self.num==1000||self.Type==MachineUnLink){
            _ringView.hidden=YES;

        }else
        {
            _editView.hidden=YES;
            _addView.hidden=YES;

        }
        [btn2 setBackgroundColor:MainColor];
        [btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btn1.backgroundColor=C(@"e8e8e8");
        [btn1 setTitleColor:C(@"5a5a5a") forState:UIControlStateNormal];
        _scrollView.contentOffset =CGPointMake(WindowWidth, 0);
        
    } forControlEvents:UIControlEventTouchUpInside];
    
    if (self.num==98||self.num==99||self.num==97||self.num==1000||self.Type==MachineUnLink){
        //从首页进来的以及 没有关联 和讨论帖来的//在待评价、已完成、已撤销状态下，没有取消关联的图标，
         _ringView=[[UIView alloc]initWithFrame:CGRectMake(WindowWidth-80, 0, 80, 64)];
        [narView addSubview:_ringView];
        UILabel *rightLab =[[UILabel alloc]initWithFrame:CGRectMake(0, 20, 65, 44)];
        rightLab.text =@"编辑";
        rightLab.textAlignment=NSTextAlignmentRight;
        rightLab.font=F(14);
        rightLab.textColor=MainColor;
        rightLab.userInteractionEnabled=YES;
    
        [_ringView addSubview:rightLab];
        [_ringView bk_whenTapped:^{
            if ([rightLab.text isEqualToString:@"编辑"]) {
                _isConext=YES;
                _sendBtn.hidden=YES;

                [rightLab setText:@"保存"];
                //名字
                UIView *bgView1=(UIView*)[_scrollView viewWithTag:100];
                //类型
                UIView *bgView2=(UIView*)[_scrollView viewWithTag:101];
                //机构
                UIView *bgView3=(UIView*)[_scrollView viewWithTag:104];
                
                bgView1.userInteractionEnabled=YES;
                bgView2.userInteractionEnabled=YES;
                bgView3.userInteractionEnabled=YES;
                //备注
                blockself.remarkView.userInteractionEnabled=YES;
                _nameTF.textColor=C(@"aeaeae");
                _nameTF.userInteractionEnabled=YES;
                _typeLab.textColor=C(@"aeaeae");
                _codeLab.textColor=C(@"aeaeae");
                _timeLab.textColor=C(@"aeaeae");
                _trameLab.textColor=C(@"aeaeae");
                _remarkView.textColor=C(@"aeaeae");
                _remarkView.userInteractionEnabled=YES;
                
            }else if ([rightLab.text isEqualToString:@"保存"])
            {
                
                [rightLab setText:@"编辑"];
                [self saveEquitInfo];
            }
        }];

    
    }else
    {
        //编辑文字
        _editView=[[UIView alloc]initWithFrame:CGRectMake(WindowWidth-80, 0, 80, 64)];
        [narView addSubview:_editView];
        _editView.hidden=YES;
        UILabel *rightLab =[[UILabel alloc]initWithFrame:CGRectMake(0, 20, 65, 44)];
        rightLab.text =@"保存";
        rightLab.textAlignment=NSTextAlignmentRight;
        rightLab.font=F(14);
        rightLab.textColor=MainColor;
        rightLab.userInteractionEnabled=YES;
        [_editView addSubview:rightLab];
        [_editView bk_whenTapped:^{
            
            [self saveEquitInfo];
            _addView.hidden=NO;
            _editView.hidden=YES;

            
        }];

        //加号
        UIImage *addImage=[UIImage imageNamed:@"Ico_detail_add"];
        _addView =[[UIView alloc]initWithFrame:CGRectMake(WindowWidth-addImage.size.width-20, 24, addImage.size.width+20, 40)];
        [narView addSubview:_addView];
        _addView.hidden=NO;
        
        UIImageView *addImg=[[UIImageView alloc]initWithFrame:CGRectMake(5, (40-addImage.size.height)/2, addImage.size.width, addImage.size.height)];
        addImg.userInteractionEnabled=YES;
        addImg.image=addImage;
        [_addView addSubview:addImg];
        [_addView bk_whenTapped:^{
            NSArray *temp = @[@{@"image" : @"Ico_ referral", @"title" : @"编辑信息"},@{@"image" : @"Ico_ referral", @"title" : @"取消关联"}];
            [SuspensionView showInputBoxbtns:temp action:^(SuspensionView *boxView, NSInteger index) {
                
                if (index==0) {
                    //编辑信息
                    _isConext=YES;
                    //名字
                    UIView *bgView1=(UIView*)[_scrollView viewWithTag:100];
                    //类型
                    UIView *bgView2=(UIView*)[_scrollView viewWithTag:101];
                    //机构
                    UIView *bgView3=(UIView*)[_scrollView viewWithTag:104];
                    bgView1.userInteractionEnabled=YES;
                    bgView2.userInteractionEnabled=YES;
                    bgView3.userInteractionEnabled=YES;
                    //备注
                    blockself.remarkView.userInteractionEnabled=YES;
                    _nameTF.textColor=C(@"aeaeae");
                    _nameTF.userInteractionEnabled=YES;
                    _typeLab.textColor=C(@"aeaeae");
                    _codeLab.textColor=C(@"aeaeae");
                    _timeLab.textColor=C(@"aeaeae");
                    _trameLab.textColor=C(@"aeaeae");
                    _remarkView.textColor=C(@"aeaeae");
                    _remarkView.userInteractionEnabled=YES;
                    _addView.hidden=YES;
                    _editView.hidden=NO;
                }else if(index==1) //取消关联
                {
                    [CClass showAlertTitle:nil hidden:NO context:@"是否取消该设备与该条任务的关联？" oneBtn:@"取消" twoBtn:@"保留" handler:^{
                        ////取消关联
                        [self requseMachineUnLink];
                    } handler:^{
                    }];
                    
                }
            }];
        }];

    }
    
    
    
    
    UIView *bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(0, narView.bounds.size.height-.5, [UIScreen mainScreen].bounds.size.width, .5)];
    [bottomLineView setBackgroundColor:LineColor];
    [narView addSubview:bottomLineView];
    self.view.backgroundColor=BackColor;
    
    
    _typeAllAr=[[NSMutableArray alloc]init];
    _dataAr=[[NSMutableArray alloc]init];
    _typeAr =[[NSMutableArray alloc]init];
    //请求设备类型
    [CClass HttpRequestTask:YES withUrl:@"select_showEquipSelect" withData:nil notifyError:YES onSuccess:^(BaseModel *model) {
        NSMutableArray* businessAr= [[NSMutableArray alloc] init];
        businessAr =[model.B_data objectForKey:@"business"];
        _typeAllAr=[model.B_data objectForKey:@"type"];
        for (int i=0; i<businessAr.count; i++) {
            NSMutableDictionary *businessDic=[[NSMutableDictionary alloc]init];
            businessDic=[businessAr objectAtIndex:i];
            NSMutableArray *typeAr=[[NSMutableArray alloc]init];
            for (int j=0; j<_typeAllAr.count; j++)
            {
                NSMutableDictionary *typeDic=[[NSMutableDictionary alloc]init];
                typeDic=[_typeAllAr objectAtIndex:j];
                if ([[businessDic objectForKey:@"state"] isEqualToString:[typeDic objectForKey:@"bussiness"]])
                {
                    [typeAr addObject:typeDic];
                    
                }
            }
            
            [_dataAr addObject:@{@"business":businessDic,@"type":typeAr}];
            
        }
        [_pickerView reloadComponent:0];
        
    } onError:^(BaseModel *model) {
        
    }];
    _bgView =[[BankView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, WindowHeight)witnOriginY:WindowHeight/3 withTitle:@"设备类型"];
    _bgView.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.4];
    [self.view bringSubviewToFront:_bgView];
    [self.view addSubview:_bgView];
    _bgView.hidden=YES;
    _pickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(0, 40, WindowWidth, WindowHeight/3-40)];
    _pickerView.delegate=self;
    _pickerView.dataSource=self;
    _pickerView.backgroundColor =[UIColor whiteColor];
    _pickerView.showsSelectionIndicator=YES;
    [_bgView.whiteView addSubview:_pickerView];
    [_bgView setSubmitBtnPress:^{
        int component0= [blockself.pickerView selectedRowInComponent:0];
        NSDictionary *dic =[blockself.dataAr objectAtIndex:component0];
        _buiness=[[dic objectForKey:@"business"] objectForKey:@"state"];
        
        int component1= [blockself.pickerView selectedRowInComponent:1];
        NSDictionary *typeDic =[blockself.typeAr objectAtIndex:component1];
        _typeStr=[typeDic objectForKey:@"desc"];
        _type=[typeDic objectForKey:@"state"];
        blockself.typeLab.textColor=C(@"aeaeae");
        blockself.typeLab.text=blockself.typeStr;
        
        
    }];
    
    _scrollView=[[TPKeyboardAvoidingScrollView alloc]initWithFrame:CGRectMake(0, 70, WindowWidth, WindowHeight-70)];
    _scrollView.contentSize=CGSizeMake(WindowWidth*2, WindowHeight-102-70);
    [self.view addSubview:_scrollView];
    _scrollView.scrollEnabled=NO;
    _scrollView.backgroundColor =BackColor;
    
    NSArray *ar=[[NSArray alloc]initWithObjects:@"设备名称",@"设备类型",@"设备编号",@"入库时间",@"所属机构",@"备注",nil];
    
    for (int i=0; i<5; i++) {
        
        UIView *bgView=[[UIView alloc]initWithFrame:CGRectMake(0, 10+44*i, WindowWidth, 44)];
        [_scrollView addSubview:bgView];
        bgView.tag=100+i;
        UILabel *nameLab = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 62, 44)];
        [bgView addSubview:nameLab];
        bgView.userInteractionEnabled=NO;
        nameLab.textColor =C(@"858585");
        nameLab.font =F(12);
        nameLab.text=[ar objectAtIndex:i];
        [CClass layoutDottedLineWithoriginY:(44-0.5) WithAddSubview:bgView];
        
        
        //        }
        if (i==0) {
            _nameTF = [[CTextField alloc] initWithFrame:CGRectMake(62+20, 0, WindowWidth-62-30, 44)];
            [_nameTF setTextAlignment:NSTextAlignmentRight];
            _nameTF.textColor=C(@"858585");
            [_nameTF setMaxLength:10 LengthChangeBlock:nil];
            [_nameTF setFont:F(11)];
            _nameTF.userInteractionEnabled=NO;
            [bgView addSubview:_nameTF];
            [_nameTF setBk_shouldChangeCharactersInRangeWithReplacementStringBlock:^BOOL(UITextField *tfd, NSRange range, NSString *str) {
                if ([[[UITextInputMode currentInputMode]primaryLanguage] isEqualToString:@"emoji"]) {
                    return NO;
                }
                return YES;
            }];
            [_nameTF bk_addEventHandler:^(id sender) {
                
                UITextPosition *position = [_nameTF positionFromPosition:[_nameTF markedTextRange] offset:0];
                if (position) {
                    return;
                }
                NSInteger maxLength = 20;
                if (maxLength > 0) {
                    NSInteger splitcount;
                    if (splitcount > 0) {
                        maxLength = maxLength + maxLength /splitcount - (maxLength % splitcount == 0 ? 1 : 0);
                    }
                    if (_nameTF.text.length > maxLength) {
                        _nameTF.text = [_nameTF.text substringWithRange:NSMakeRange(0, maxLength)];
                    }
//                    //移除表情
//                    NSString *newStr= [CClass disable_emoji:_nameTF.text];
//                    _nameTF.text=newStr;
                    
                    
                }
                
            } forControlEvents:UIControlEventEditingChanged];
        }else if(i==1)
        {
            _typeLab = [[UILabel alloc] initWithFrame:CGRectMake(62+20, 0, WindowWidth-62-30, 44)];
            [_typeLab setTextAlignment:NSTextAlignmentRight];
            _typeLab.textColor=C(@"858585");
            [_typeLab setFont:F(11)];
            [bgView addSubview:_typeLab];

            
            [bgView bk_whenTapped:^{
                
                [self.view endEditing:YES];
                
                _bgView.hidden=NO;
                [self.view bringSubviewToFront:_bgView];
                int i = 0;
                for (NSDictionary *dict in _dataAr) {
                    BOOL isBreak;
                    _typeAr=[dict objectForKey:@"type"];
                    [_pickerView reloadComponent:1];
                    for (int j=0; j<_typeAr.count; j++) {
                        NSDictionary *typeDic=[[NSDictionary alloc]init];
                        typeDic=[_typeAr objectAtIndex:j];
                        if ([[typeDic objectForKey:@"desc"] isEqualToString:_typeStr]) {
                            
                            [_pickerView selectRow:i inComponent:0 animated:YES];
                            [_pickerView selectRow:j inComponent:1 animated:YES];
                            isBreak=YES;
                            break;
                        }
                        
                    }
                    if (isBreak==YES) {
                        break;
                    }
                    i ++;
                }
                if (i == _dataAr.count) {
                    NSDictionary *dic =[_dataAr objectAtIndex:0];
                    _buiness=[[dic objectForKey:@"business"] objectForKey:@"state"];
                    _typeAr=[dic objectForKey:@"type"];
                    [_pickerView reloadComponent:1];
                    _typeStr=[[_typeAr objectAtIndex: 0] objectForKey:@"desc"];
                    _type=[[_typeAr objectAtIndex: 0] objectForKey:@"state"];
                }
                
                
                [UIView animateWithDuration:0.5 animations:^{
                    _bgView.whiteView.frame=CGRectMake(0,WindowHeight/3*2 , WindowWidth, WindowHeight/3);
                } completion:nil];
                
                
                
            }];
            
        }else if(i==2)
        {
            _codeLab = [[UILabel alloc] initWithFrame:CGRectMake(62+20,0, WindowWidth-62-30, 44)];
            [_codeLab setTextAlignment:NSTextAlignmentRight];
            _codeLab.textColor=C(@"858585");
            [_codeLab setFont:F(11)];
            [bgView addSubview:_codeLab];
            
        }else if(i==3)
        {
            _timeLab = [[UILabel alloc] initWithFrame:CGRectMake(62+20, 0, WindowWidth-62-30, 44)];
            [_timeLab setTextAlignment:NSTextAlignmentRight];
            _timeLab.textColor=C(@"858585");
            [_timeLab setFont:F(11)];
            [bgView addSubview:_timeLab];
            
        } else if (i==4)
        {
            
            _trameLab = [[UILabel alloc] initWithFrame:CGRectMake(62+20, 0, WindowWidth-62-30, 44)];
            [_trameLab setTextAlignment:NSTextAlignmentRight];
            _trameLab.textColor=C(@"858585");
            [_trameLab setFont:F(11)];
            [bgView addSubview:_trameLab];
            _trameLab.userInteractionEnabled=NO;

            [bgView bk_whenTapped:^{
                ScreentoneViewController *screenVC=[[ScreentoneViewController alloc]init];
                screenVC.backuiud=^(NSMutableDictionary*dic){
                    _trameLab.text =[dic objectForKey:@"name"];
                    _trame =[dic objectForKey:@"uuid"];
                    
                };
                [self pushController:screenVC];
                
            }];
        }
        
    }
    _textBGView=[[UIView alloc]initWithFrame:CGRectMake(0, 10+44*5, WindowWidth, 100)];
    [_scrollView addSubview:_textBGView];

    UILabel *nameLab = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 62, 44)];
    [_textBGView addSubview:nameLab];
    nameLab.textColor =C(@"858585");
    nameLab.font =F(12);
    nameLab.text=@"备注";
    
    _remarkView = [[UITextView alloc] initWithFrame:CGRectMake(62+20, 0, WindowWidth-62-30, 100)];
    _remarkView.textAlignment =NSTextAlignmentRight;
    _remarkView.backgroundColor=[UIColor clearColor];
    _remarkView.delegate=self;
    _remarkView.userInteractionEnabled=NO;
    [_remarkView setTextColor:C(@"858585")];
    [_remarkView setFont:F(12)];
    [_textBGView addSubview:_remarkView];
    _sendBtn.frame =CGRectMake(44,CGRectGetMaxY(_textBGView.frame)+10, WindowWidth-88, 38);

    //设备信息
    [CClass HttpRequestTask:YES withUrl:@"machine_detail" withData:@{@"uuid":self.machineUuid} notifyError:YES onSuccess:^(BaseModel *model) {
        if([F_NSpace([model.B_data objectForKey:@"name"]) isEqualToString:@""]){
            self.nameTF.text=@"";
            
        }else
        {
            self.nameTF.text=[model.B_data objectForKey:@"name"];
            
        }
        if([F_NSpace([model.B_data objectForKey:@"type_name"]) isEqualToString:@""]){
            self.typeLab.text=@"";
            
        }else
        {
            self.typeLab.text=[model.B_data objectForKey:@"type_name"];
            _type=[model.B_data objectForKey:@"type_cd"];
            
        }
        if([F_NSpace([model.B_data objectForKey:@"code"]) isEqualToString:@""]){
            self.codeLab.text=@"";
            
        }else
        {
            self.codeLab.text=[model.B_data objectForKey:@"code"];
            
        }
        if([F_NSpace([model.B_data objectForKey:@"created"]) isEqualToString:@""]){
            self.timeLab.text=@"";
            
        }else
        {
            NSString  *a = [model.B_data objectForKey:@"created"];
            NSString *timeString =  [a substringWithRange:NSMakeRange(0, 16)];
            self.timeLab.text=timeString;
            
        }
        if([F_NSpace([model.B_data objectForKey:@"inst_name"]) isEqualToString:@""]){
            self.trameLab.text=@"";
            
        }else
        {
            self.trameLab.text=[model.B_data objectForKey:@"inst_name"];
            _trame=[model.B_data objectForKey:@"inst"];
            
        }
        _buiness=[model.B_data objectForKey:@"bussines"];
        //备注
        if([F_NSpace([model.B_data objectForKey:@"remark"]) isEqualToString:@""]){
            self.remarkView.text=@"";
            _textBGView=[[UIView alloc]initWithFrame:CGRectMake(0, 10+44*5, WindowWidth, 100)];
            self.remarkView.frame=CGRectMake(62+20, 0, WindowWidth-62-30,100);
            _sendBtn.frame =CGRectMake(44,CGRectGetMaxY(_textBGView.frame)+10, WindowWidth-88, 38);

        }else
        {
            
            CGSize  size = [[model.B_data objectForKey:@"remark"] sizeWithFont:FF(14) constrainedToSize:CGSizeMake(WindowWidth-62-30, 900)];
            float numLine=ceilf(size.width/_remarkView.bounds.size.width); //计算当前文字长度对应的行数
            if (numLine>=2) {
                _remarkView.textAlignment=NSTextAlignmentLeft;
                
            }else if (numLine==1)
            {
                _remarkView.textAlignment=NSTextAlignmentRight;
                
            }
            
            if (size.height>100) {
                _textBGView=[[UIView alloc]initWithFrame:CGRectMake(0, 10+44*5, WindowWidth, size.height+20)];
                self.remarkView.frame=CGRectMake(62+20, 0, WindowWidth-62-30,size.height+10);
                _sendBtn.frame =CGRectMake(44,CGRectGetMaxY(_textBGView.frame)+10, WindowWidth-88, 38);

            }else
            {
                _textBGView=[[UIView alloc]initWithFrame:CGRectMake(0, 10+44*5, WindowWidth, 100)];
                self.remarkView.frame=CGRectMake(62+20, 0, WindowWidth-62-30,100);
               _sendBtn.frame =CGRectMake(44,CGRectGetMaxY(_textBGView.frame)+10, WindowWidth-88, 38);

                
            }
            
            self.remarkView.text=[model.B_data objectForKey:@"remark"];
            
        }
        
        
        
        
    } onError:^(BaseModel *model) {
        
    }];

     _sendBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    [_sendBtn setBackgroundColor:MainColor];
    _sendBtn.layer.masksToBounds=YES;
    _sendBtn.layer.cornerRadius=8;

    [_sendBtn setTitle:@"关联设备" forState:UIControlStateNormal];
    _sendBtn.titleLabel.font =F(15);
    [_sendBtn bk_addEventHandler:^(id sender) {
        //关联设备的请求
        [self requsetMachineInput];
        
    } forControlEvents:UIControlEventTouchUpInside];
    [_scrollView addSubview:_sendBtn];
    
    if(self.Type==MachineLink)//不需要关联
    {
        _sendBtn.hidden=YES;

    }else if(self.Type==MachineUnLink)//需要关联
    {
        _sendBtn.hidden=NO;

    }
    //维护记录
    HistoryView *histroyView =[[HistoryView alloc]initWithFrame:CGRectMake(WindowWidth, 10, WindowWidth, _scrollView.bounds.size.height-10) withWarningUuid:self.machineUuid];
    [_scrollView addSubview:histroyView];
    [_scrollView bringSubviewToFront:_sendBtn];
    

}
#pragma mark关联设备的请求
- (void)requsetMachineInput
{
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    [dic setObject:self.orderUuid forKey:@"orderUuid"];
    [dic setObject:self.machineUuid forKey:@"machineUuid"];
    
    [CClass HttpRequestTask:YES withUrl:@"machine_machineLink" withData:dic notifyError:YES onSuccess:^(BaseModel *model) {
        _isMachineLink=YES;
        [CClass showLittleTimeMessage:@"关联设备成功"completion:^{
            [self backController];
            
        }];
        
    } onError:^(BaseModel *model) {
        
        
    }];
    
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
    
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 35;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component==0) {
        return _dataAr.count;
        
    }else
    {
        return _typeAr.count;
    }
}
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component==0) {
        NSDictionary *dic =[_dataAr objectAtIndex:row];
        return [[dic objectForKey:@"business"] objectForKey:@"desc"];
    }else
    {
        NSDictionary *dic =[_typeAr objectAtIndex:row];
        return [dic objectForKey:@"desc"];
    }
    
    
    
    
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component==0) {
        NSDictionary *dic =[_dataAr objectAtIndex:row];
        _buiness=[[dic objectForKey:@"business"] objectForKey:@"state"];
        _typeAr=[dic objectForKey:@"type"];
        [_pickerView reloadComponent:1];
        
        
    }else
    {
        
    }
    
    
    
}
- (void)textViewDidChange:(UITextView *)textView{
    if ([CClass stringContainsEmoji:textView.text]) {
        textView.text=[textView.text substringToIndex:textView.text.length-2];
        return;
    }
    
    if (textView.markedTextRange == nil && textView.text.length > 200)  //加上 textView.markedTextRange == nil判断，当此属性为nil时，代表不在这种联想输入等待确定状态。
        
    {
        
        textView.text = [textView.text substringToIndex:200];
        
    }
    
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    
    if ([[[UITextInputMode currentInputMode]primaryLanguage] isEqualToString:@"emoji"]) {
        return NO;
    }

    
    return YES;
    
}
//保存设备信息
- (void)saveEquitInfo
{
    _isConext=NO;
    //名字
    UIView *bgView1=(UIView*)[_scrollView viewWithTag:100];
    //类型
    UIView *bgView2=(UIView*)[_scrollView viewWithTag:101];
    //机构
    UIView *bgView3=(UIView*)[_scrollView viewWithTag:104];
    
    bgView1.userInteractionEnabled=NO;
    bgView2.userInteractionEnabled=NO;
    bgView3.userInteractionEnabled=NO;
    self.remarkView.userInteractionEnabled=NO;
    _nameTF.textColor=C(@"858585");
    _nameTF.userInteractionEnabled=NO;
    _typeLab.textColor=C(@"858585");
    _codeLab.textColor=C(@"858585");
    _timeLab.textColor=C(@"858585");
    _trameLab.textColor=C(@"858585");
    _remarkView.textColor=C(@"858585");
    _remarkView.userInteractionEnabled=NO;
    if ([self.nameTF.text isEqualToString:@""]) {
        [CClass showMessage:@"请填写设备名称!"];
        return ;
    }
    if (_type==nil) {
        [CClass showMessage:@"请选择设备类型!"];
        return ;
    }
    if (_trame==nil) {
        [CClass showMessage:@"请选择所属机构!"];
        return ;
    }
    
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    [dic setObject:self.machineUuid forKey:@"uuid"];
    [dic setObject:_nameTF.text forKey:@"name"];
    [dic setObject:_type forKey:@"type_cd"];
    [dic setObject:_trame forKey:@"inst"];
    [dic setObject:_buiness forKey:@"bussines"];
    if ([_remarkView.text isEqualToString:@""]) {
        [dic setObject:@"" forKey:@"remark"];
        
    }else
    {
        [dic setObject:_remarkView.text forKey:@"remark"];
        
    }
    [self setIsLoading:YES];
    [CClass HttpRequestTask:YES withUrl:@"machine_machineEdit" withData:dic notifyError:YES onSuccess:^(BaseModel *model) {
        [CClass showLittleTimeMessage:@"保存成功" completion:^{
            
            [self setIsLoading:NO];
        }];
        
    } onError:^(BaseModel *model) {
        [self setIsLoading:NO];

    }];

}
#pragma mark 取消关联的请求
- (void)requseMachineUnLink
{
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    [dic setObject:self.machineUuid forKey:@"machineUuid"];
    [dic setObject:self.orderUuid forKey:@"orderUuid"];
    [CClass HttpRequestTask:YES withUrl:@"machine_machineUnLink" withData:dic notifyError:YES onSuccess:^(BaseModel *model) {
        [CClass showLittleTimeMessage:@"取消关联成功" completion:^{
            [self backController];

        }];
    } onError:^(BaseModel *model) {
        
    }];
}
@end
